package com.yg.ssm.index.serv;

import com.alibaba.fastjson.JSONObject;
import com.yg.ssm.index.dao.MenuTreeDao;
import com.yg.ssm.index.model.MenuTreeModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/29.
 */
@Service("menuTreeS")
public class MenuTreeService {
    @Autowired
    private MenuTreeDao menuTreeDao;

    public void setMenuTreeDao(MenuTreeDao menuTreeDao) {
        this.menuTreeDao = menuTreeDao;
    }

    /*
    * 根据Pid查询所有子菜单数据
    * **/
    public List<JSONObject> queryMenuTreeList(String pid) {
        List<MenuTreeModel> menuData = menuTreeDao.queryMenu(pid);
        List<JSONObject> jsonTreeData = buildParentNode(menuData);
        return jsonTreeData;
    }

    protected List<JSONObject> buildParentNode(List<MenuTreeModel> menuData) {
        List<JSONObject> jsonList = new ArrayList<JSONObject>();
        JSONObject obj;
        List<JSONObject> childObj;
        for (MenuTreeModel mo : menuData) {
            obj = new JSONObject();
            obj.put("id", mo.getMid());
            obj.put("text", mo.getMenu_name());
            childObj = buildChildrenNode(mo.getMid());
            obj.put("children", childObj);
            obj.put("data", mo);
            jsonList.add(obj);
        }
        return jsonList;
    }

    protected List<JSONObject> buildChildrenNode(String pid) {
        List<MenuTreeModel> menuData = menuTreeDao.queryMenu(pid);
        return buildParentNode(menuData);
    }

}
